package org.hackystat.sdsa.analyzer.model.activity;

import java.util.Map;
import java.util.logging.Logger;

import jess.Fact;
import jess.JessException;
import jess.RU;
import jess.Rete;
import jess.Value;

import org.hackystat.sdsa.analyzer.control.SensorDataUtil;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;

/**
 * Implements a compilation activity.
 * 
 * @author hongbing
 *
 */
public class CompilationActivity extends Activity {
  /** Compilation error. */
  private String errMsg;

  /** Check successfulness of this compilation activity. */
  private boolean successful;
  
  /** Success or not. */
  public static final String SUCCESS = "success";
  /** Error message. */
  public static final String ERROR = "error";
  /** Activity Type */
  private static final String TYPE = "COMPILE";

  /**
   * Instantiates an instance.
   * 
   * @param sensorData Sensor data instance.
   * @param logger Logger. 
   */
  public CompilationActivity(SensorData sensorData, Logger logger) {
    super(sensorData, logger);
    super.setType(TYPE);
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);
    String successString = propMap.get(SUCCESS);
    this.successful = Boolean.parseBoolean(successString);
    this.errMsg = propMap.get(ERROR);
  }
  
  /** Get the representation of this activity. 
   * 
   * @return String representation of this activity.
   */
  @Override public String getRepresentation() {
    return this.errMsg;
  }
  
  /**
   * Whether compilation is successful or not.
   * 
   * @return True if successful.
   */
  public boolean isSuccessful() {
    return this.successful;
  }
  
  /**
   * Sets error message.
   * 
   * @param errMsg Error message.
   */
  public void setErrorMessage(String errMsg) {
    this.errMsg = errMsg;    
  }

  /**
   * Gets the compilation error message.
   * 
   * @return Compilation error message.
   */
  public String getErrorMessage() {
    return this.errMsg;
  }
  
  /**
   * Makes Jess facts in the given Jess rete engine.
   * 
   * @param index Action index in episode. 
   * @param engine Jess rete engine.
   * @throws JessException If error while constructing jess action.
   * @return Jess fact of this action.
   */
  @Override public Fact assertActivity(int index, Rete engine) throws JessException  {
    Fact f = new Fact("CompilationActivity", engine);
    f.setSlotValue(INDEX_SLOT, new Value(index, RU.INTEGER));
    String file = super.extractFileName(super.getResourceUri());
    f.setSlotValue(FILE_SLOT, new Value(file, RU.STRING));
    f.setSlotValue("message", new Value(this.getErrorMessage(), RU.STRING));
    Fact assertedFact = engine.assertFact(f);
    
    return assertedFact;
  }  
  
  /**
   * Gets the compilation action string.
   * 
   * @return Compilation action string.
   */
  @Override public String toString() {
    StringBuffer buf = new StringBuffer();
    buf.append(super.toString());
    buf.append(" {").append(this.errMsg).append('}'); 
    return buf.toString();
  }
}